package com.xy.commons;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import sun.misc.BASE64Encoder;

import com.mortennobel.imagescaling.ResampleOp;

public class ImgUtil {
	public BufferedImage readImage(InputStream is) {
		BufferedImage image = null;
		try {
			image = ImageIO.read(is);
		} catch (IOException e) {
		}
		return image;
	}
	public BufferedImage readImage(File file) {
		BufferedImage image = null;
		if (file != null && file.isFile() && file.exists()) {
			try {
				image = ImageIO.read(file);
			} catch (IOException e) {
			}
		}
		return image;
	}
	
	/**
	 * get the image width
	 * 
	 * @param image
	 * @return image width
	 */
	public double getWidth(BufferedImage image) {
		return image.getWidth();
	}

	/**
	 * get the image height
	 * 
	 * @param image
	 * @return image height
	 */
	public double getHeight(BufferedImage image) {
		return image.getHeight();
	}
	
	/**
	 * @param image
	 *            BufferedImage.
	 * @param width
	 *            zoom width.
	 * @param heigth
	 *            zoom heigth.
	 * @return BufferedImage
	 */
	public BufferedImage zoom(BufferedImage image, int width, int heigth) {
		ResampleOp resampleOp = new ResampleOp(width, heigth);
		BufferedImage tag = resampleOp.filter(image, null);
		return tag;
	}

	 
	/**
	 * 
	 * Output to file out according to the style<code>BufferedImage</code> If
	 * can not appoint image or formateName or file out ,do nothing. Now it can
	 * support image type��bmp,wbmp,jpeg,png.
	 * 
	 * @param image
	 *            BufferedImage.
	 * @param formatName
	 *            format name.
	 * @param out
	 *            output path.
	 * @throws IOException
	 *             IOException
	 */
	public void writeImage(BufferedImage image, String formatName, File file) throws IOException {
		if (image != null && formatName != null && !"".equals(formatName) && file != null) {
			ImageIO.write(image, formatName, file);
		}
	}
	
	/**
	 * 
	 * @param image
	 *            BufferedImage
	 * @param width
	 *            zoom width.
	 * @param heigth
	 *            zoom heigth.
	 * @return double array.double[0]:width,double[1]:heigth.
	 */
	public double[] zoomSize(BufferedImage image, int width, int heigth) {
		double[] zoomSize = new double[2];
		double srcWidth = getWidth(image);
		double srcHeigth = getHeight(image);
		if (srcWidth > srcHeigth) {
			zoomSize[0] = (srcWidth / srcHeigth) * heigth;
			zoomSize[1] = heigth;
		}
		if (srcWidth < srcHeigth) {
			zoomSize[0] = width;
			zoomSize[1] = (srcHeigth / srcWidth) * width;
		}
		return zoomSize;
	}
	  /**
     * Encode image to Base64 string
     * @param image The image to Base64 encode
     * @param type jpeg, bmp, ...
     * @return Base64 encoded string
     */
    public static String encodeToString(BufferedImage image, String type) {
        String imageString = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, type, bos);
            byte[] imageBytes = bos.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            imageString = encoder.encode(imageBytes);
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageString;
    }
    
    public static void main(String args[])throws Exception{
    	ImgUtil m = new ImgUtil();
    	InputStream is = new FileInputStream("");
    	BufferedImage image = m.readImage(is);
    	double[] size = m.zoomSize(image, 29, 29);
		m.writeImage(m.zoom(image, (int) size[0], (int) size[1]), "jpes", new File("outfile"));
    }
}
